package com.wms.token.proxys;

import java.io.IOException;
import java.io.PrintWriter;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.context.WebApplicationContext;

import com.wms.core.token.login.TokenLogin;


public class CheckIN extends HttpServlet{
	
private static final long serialVersionUID = 1L;
	
	private static final Logger logger = Logger.getLogger(CheckIN.class);
	
	private static WebApplicationContext  wac;
	
	private String nam;
	private String pwd;
	private int code;
	
	@Resource
	private TokenLogin util;
	
	public CheckIN(){
		super();
	}
	
	public static void setWac(WebApplicationContext wac){
		CheckIN.wac = wac;
	}
	
	public void setUtil(TokenLogin util) {
		this.util = util;
	}

	public void doRun(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
		doPost(request,response);
	}
	
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.debug("Proxy CheckIN : doGet");
		doPost(request,response);
	}
	
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		try{
			
			code = util.validateLogInfor(request.getParameter("nam"), request.getParameter("pwd"), request.getSession().getId());
			
			
			String resCont = String.valueOf(code);
			
			response.setHeader("Access-Control-Allow-Origin", "*");
			response.setContentType("application/x-www-form-urlencoded");
			response.setCharacterEncoding("UTF-8");
			PrintWriter out = response.getWriter(); 
			
			out.print(resCont);//将json string 放入response的回复中
			out.flush();//response 发送
			
			logger.debug("返回结果 : "+resCont);
			logger.debug("response 处理完毕");
			
		}catch (Exception e) {
			
			e.printStackTrace();
			response.setStatus(404);
		} 
		
	}
	
	
	
}
